package eclipse.lib.map;
import eclipse.*;
import eclipse.lib.scripting.*;
public class MapFile extends ByteDecoder
{
	//private byte[]		mapData;
	private ByteArray	mapData;
	private int			layerCount;
	private String		name;
	private String		music;
	private int			mapLeft;
	private int			mapRight;
	private int			mapUp;
	private int			mapDown;
	private int			respawnMap;
	private int			respawnX;
	private int			respawnY;
	private Layer[]		layer;
	public static void main(String args[])
	{		
		MapFile map = new MapFile();		
		map.writeMusic("music");
		map.writeName("name");
		map.writeLayerCount(1);
		map.writeLayerDim(0, 3, 3);
		map.writeLayerTileSheet(0, 2, 1, 1);
		map.decode(map.getData());
		
		System.out.println(map.getLayer(0).getTileSheet(1, 1));
		System.out.println(map.getLayerCount());
		System.out.println(map.getMapMusic());
		System.out.println(map.getMapName());
	}
	public void parser(ByteArray comCode)
	{		
		byte[] command = comCode.toArray();
		if(command[0] 		== Constants.com_MAPSETLAYERS)
			readLayerCount(command);
		else if(command[0] 	== Constants.com_MAPSETNAME)
			readName(command);
		else if(command[0]  == Constants.com_MAPSETMUSIC)
			readMusic(command);
		else if(command[0]  == Constants.com_MAPSETLEFT)
			readMapLeft(command);
		else if(command[0]  == Constants.com_MAPSETRIGHT)
			readMapRight(command);
		else if(command[0]  == Constants.com_MAPSETUP)
			readMapUp(command);
		else if(command[0]  == Constants.com_MAPSETDOWN)
			readMapDown(command);
		else if(command[0]  == Constants.com_MAPSETRESPAWNMAP)
			readRespawnMap(command);
		else if(command[0]  == Constants.com_MAPSETRESPAWNX)
			readRespawnX(command);
		else if(command[0]  == Constants.com_MAPSETRESPAWNY)
			readRespawnY(command);
		else if(command[0]  == Constants.com_MAPSETLAYERMAX)
			readLayerDim(command);
		else if(command[0]  == Constants.com_MAPSETLAYERSCRIPT)
			readLayerScript(command);
		else if(command[0]  == Constants.com_MAPSETLAYERSHEET)
			readLayerTileSheet(command);
		else if(command[0]  == Constants.com_MAPSETLAYERTILEX)
			readLayerTileX(command);
		else if(command[0]  == Constants.com_MAPSETLAYERTILEY)
			readLayerTileY(command);
	}
	public MapFile()
	{
		mapData			= new ByteArray();
	}
	public void writeLayerTileX(int layerID, int xVal, int yVal, int tVal)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERTILEX);
		mapData.appendTo(xVal);
		mapData.appendTo(yVal);
		mapData.appendTo(layerID);
		mapData.appendTo(tVal);
		mapData.appendTo(Constants.com_END);
	}
	public void writeLayerTileY(int layerID, int xVal, int yVal, int tVal)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERTILEY);
		mapData.appendTo(xVal);
		mapData.appendTo(yVal);
		mapData.appendTo(layerID);
		mapData.appendTo(tVal);
		mapData.appendTo(Constants.com_END);
	}
	public void writeLayerTileSheet(int layerID, int tileSheet, int xVal, int yVal)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERSHEET);
		mapData.appendTo(tileSheet);
		mapData.appendTo(layerID);
		mapData.appendTo(xVal);
		mapData.appendTo(yVal);
		mapData.appendTo(Constants.com_END);
	}
	public void writeLayerColor(int layerID, float red, float blue, float green)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERCOLOR);
		mapData.appendTo(layerID);
		mapData.appendTo(red);
		mapData.appendTo(blue);
		mapData.appendTo(green);
		mapData.appendTo(Constants.com_END);
	}
	public void writeLayerCount(int layer)
	{		
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERS);
		mapData.appendTo(layer);
		mapData.appendTo(Constants.com_END);
	}
	public void writeMapLeft(int left)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLEFT);
		mapData.appendTo(left);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeMapRight(int right)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETRIGHT);
		mapData.appendTo(right);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeRespawnMap(int map)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETRESPAWNMAP);
		mapData.appendTo(map);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeRespawnX(int x)
	{
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETRESPAWNX);
		mapData.appendTo(x);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeRespawnY(int y)
	{
		mapData.appendTo(Constants.com_START);		
		mapData.appendTo(Constants.com_MAPSETRESPAWNY);
		mapData.appendTo(y);
		mapData.appendTo(Constants.com_END);
	}	
	public void writeLayerDim(int layerID, int xVal, int yVal)
	{		
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERMAX);
		mapData.appendTo(xVal);
		mapData.appendTo(yVal);
		mapData.appendTo(layerID);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeLayerScript(int layerID, int xVal, int yVal, String nScript)
	{
		char[] cScript	= new char[Constants.STRINGSIZE];
		
		for(int i = 0; i < cScript.length; i++)
		{
			cScript[i] = ' ';
			if(i < nScript.length())
				cScript[i] = nScript.charAt(i);
		}
		nScript 	= new String(cScript);
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETLAYERSCRIPT);
		mapData.appendTo(xVal);
		mapData.appendTo(yVal);
		mapData.appendTo(layerID);
		mapData.appendTo(nScript);
		mapData.appendTo(Constants.com_END);		
	}
	public void writeName(String nName)
	{
		char[] cName 	= new char[Constants.STRINGSIZE];				
		for(int i = 0; i < cName.length; i++)
		{
			cName[i] = ' ';
			if(i < nName.length())
				cName[i] = nName.charAt(i);			
		}
		
		nName 		= new String(cName);
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETNAME);
		mapData.appendTo(nName);
		mapData.appendTo(Constants.com_END);
	}
	public void writeMusic(String nMusic)
	{
		char[] cMusic 	= new char[Constants.STRINGSIZE * 4];
		
		for(int i = 0; i < cMusic.length; i++)
		{
			cMusic[i] = ' ';
			if(i < nMusic.length())
				cMusic[i] = nMusic.charAt(i);			
		}
		
		nMusic 		= new String(cMusic);
		mapData.appendTo(Constants.com_START);
		mapData.appendTo(Constants.com_MAPSETMUSIC);
		mapData.appendTo(nMusic);
		mapData.appendTo(Constants.com_END);		
	}	
	private void readLayerTileSheet(byte[] command)
	{
		int sheet 	= ByteInterpreter.byteArrayToInt(command, 1);
		int layerID	= ByteInterpreter.byteArrayToInt(command, 5);
		int xVal	= ByteInterpreter.byteArrayToInt(command, 9);
		int yVal	= ByteInterpreter.byteArrayToInt(command, 13);
		layer[layerID].setTileSheet(xVal, yVal, sheet);
	}
	private void readLayerTileY(byte[] command)
	{
		int xVal = ByteInterpreter.byteArrayToInt(command, 1);
		int yVal = ByteInterpreter.byteArrayToInt(command, 5);
		int layerID = ByteInterpreter.byteArrayToInt(command, 9);
		int tVal = ByteInterpreter.byteArrayToInt(command, 13);
		
		layer[layerID].setTileY(xVal, yVal, tVal);
	}
	private void readLayerTileX(byte[] command)
	{
		int xVal = ByteInterpreter.byteArrayToInt(command, 1);
		int yVal = ByteInterpreter.byteArrayToInt(command, 5);
		int layerID = ByteInterpreter.byteArrayToInt(command, 9);
		int tVal = ByteInterpreter.byteArrayToInt(command, 13);
		
		layer[layerID].setTileX(xVal, yVal, tVal);
	}
	private void readLayerCount(byte[] command)
	{
		layerCount = ByteInterpreter.byteArrayToInt(command, 1);
		layer 		= new Layer[layerCount];
		for(int i = 0; i < layerCount; i++)
			layer[i] = new Layer();
	}
	private void readName(byte[] command)
	{			
		command = ByteInterpreter.getSubByteArray(command, 1, command.length);
		name 	= ByteInterpreter.byteArrayToString(command);
	}
	private void readMusic(byte[] command)
	{		
		command = ByteInterpreter.getSubByteArray(command, 1, command.length);
		music 	= ByteInterpreter.byteArrayToString(command);
	}
	private void readMapLeft(byte[] command)
	{		
		mapLeft	= ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readMapRight(byte[] command)
	{
		mapRight = ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readMapUp(byte[] command)
	{
		mapUp = ByteInterpreter.byteArrayToInt(command, 1);		
	}
	private void readMapDown(byte[] command)
	{
		mapDown = ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readRespawnMap(byte[] command)
	{
		respawnMap = ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readRespawnX(byte[] command)
	{
		respawnX = ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readRespawnY(byte[] command)
	{
		respawnY = ByteInterpreter.byteArrayToInt(command, 1);
	}
	private void readLayerDim(byte[] command)
	{
		int id, x, y;
		x	= ByteInterpreter.byteArrayToInt(command, 1);
		y	= ByteInterpreter.byteArrayToInt(command, 5);
		id	= ByteInterpreter.byteArrayToInt(command, 9);
		layer[id].make(x, y);
	}
	private void readLayerScript(byte[] command)
	{
		int id, x, y;
		String s;
		x	= ByteInterpreter.byteArrayToInt(command, 1);
		y	= ByteInterpreter.byteArrayToInt(command, 5);
		id	= ByteInterpreter.byteArrayToInt(command, 9);
		s	= ByteInterpreter.byteArrayToString(ByteInterpreter.getSubByteArray(command, 13, 13 + Constants.STRINGSIZE));
		layer[id].setScript(x, y, s);
	}
	public int getLayerCount()
	{
		return layerCount;
	}
	public String getMapMusic()
	{
		return music;
	}
	public String getMapName()
	{
		return name;
	}
	public int getMapLeft()
	{
		return mapLeft;
	}
	public int getMapRight()
	{
		return mapRight;
	}
	public int getMapUp()
	{
		return mapUp;
	}
	public int getMapDown()
	{
		return mapDown;
	}
	public int getRespawnMap()
	{
		return respawnMap;
	}
	public int getRespawnX()
	{
		return respawnX;
	}
	public int getRespawnY()
	{
		return respawnY;
	}	
	public Layer getLayer(int layerID)
	{
		return layer[layerID];
	}
	public ByteArray getData()
	{
		return mapData;
	}
}
